package com.garen.orm;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.garen.utils.CommonUtils;
import com.garen.utils.FileUtils;
//import com.garen.utils.doc.PdfCreator;

public class OrmTest {
 
	protected static Log log = LogFactory.getLog(OrmTest.class);
	private static int count = 0,count1 = 0,count2 = 0;
	//保存统计结果
	//private static Map<String,Integer> sum = new HashMap<>();
	
	private static Map<String,String> map = null;
	private static Map<String,String> enMap = null;
	private static StringBuilder sb = new StringBuilder();
	
	public static void main(String[] args) {
		String path = "C:\\javadev\\201606\\consumeweb\\src\\main\\webapp\\js\\test\\pdf.json";
		//PdfCreator pdfCreator = new PdfCreator();
		String jsonStr = new String(FileUtils.readData(path));
		//log.debug(jsonStr);
		JSONObject jobj = JSON.parseObject(jsonStr);
		//if(jobj != null) pdfCreator.createPdf(jobj,null,null,null);
		log.debug("生成pdf文档。");
		log.debug(jobj.toJSONString());
	}
	
	public static void test4(){
		String basePath = "D:\\javadev\\temp\\wedsdoor";
		bfile(new File(basePath));
		System.out.println("文件总数:" + count);
		System.out.println("词句总数:" + count2);
		System.out.println("匹配总数:" + count1);
	}
	
	public static void test5(){
		//printlog("hello");
		String sql = "select  t.code_name,t.code_desc3,t.code_value,t.code_desc4,t.code_type,t.code_desc2,t.id,t.code_desc1,"
				+ "( select top 1  tt.code_name from sys_code tt  where tt.code_value=t.code_type and tt.code_type='1' ) as code_type_name ,"
				+ "( select top 1  tt.code_name from sys_code tt  where tt.code_value=t.code_type and tt.code_type='1'  ) as code_type_name333 "
				+ " from sys_code t  where t.code_type=:code_type and from aa";
		//printlog(CommonUtils.regx(sql, ""));
		sql = sql.replaceAll("\\([^\\)]+\\)\\s+as", "");
		printlog(sql);
		printlog(CommonUtils.regx(sql, "select\\s+(.+?)\\s+from"));
	}
	
	public static void test3(){
		Set<String> strs =  regx("aa=\"汉字\"  bb=\"5可kl你6\"  cc=\"我6\"","\"([^\"\n]*[\u4e00-\u9fa5][^\"\n]+)\"");
		for(String str : strs)
			System.out.println(str);
	}
	
	public static void test1(){
		doFile("D:\\javadev\\20150701\\wedsdoor\\WebContent\\WEB-INF\\report\\mjjlDepOrDev.jsp");
	}
	
	public static void test2(){
		String basePath = "D:\\javadev\\temp\\wedsdoor";
		bfile(new File(basePath));
		System.out.println("文件总数:" + count);
		System.out.println("词句总数:" + count2);
		System.out.println("匹配总数:" + count1);
		//System.out.println(sb.toString());
		/*
		for(String key : sum.keySet()){
			System.out.println(sum.get(key) + "," + key);
		}*/
	}
	
	//验证文件扩展名是否为.java .js  .jsp
	public static boolean checkFile(String fileName){
		String ext = FileUtils.getExtName(fileName);
		if(".java".equals(ext) || 
				".js".equals(ext) || 
				".jsp".equals(ext)) return true;
		return false;
	}
	
	public static void bfile(File file){
		File[] files = file.listFiles();
		for(File f : files){
			if(f.isDirectory()) bfile(f);
			else{
				if(checkFile(f.getName().toLowerCase()) == false)
					continue;
				count++;
				StringBuilder sbFile = doFile(f.getPath());
				if(sbFile.length() > 0){
					sb.append("##" + f.getPath() + "\n");
					sb.append(sbFile);
				}
			}
		}
	} 
	
	public static StringBuilder doFile(String filePath){
		byte[] body = FileUtils.readData(filePath);
		StringBuilder sbFile = new StringBuilder();
		try {
			String content = new String(body,"utf-8");
			Set<String> strs = regx(content,"\"([^\"\n']*[\u4e00-\u9fa5][^\"\n']+)\"");
			Set<String> strs1 = regx(content,"'([^\'\n]*[\u4e00-\u9fa5][^\'\n]+)'");
			Set<String> strs2 = regx(content,">([\u4e00-\u9fa5][^<\n]+)<");
			strs.addAll(strs1);
			strs.addAll(strs2);
			for(String str : strs){
				sbFile.append(str +  "\n");
				if(map.containsKey(str)){
					count1++;
					sbFile.append(enMap.get(map.get(str)) +  "\n");
				}else{
					sbFile.append("\n");//空行
				}
				sbFile.append("\n");//空行
				count2++;
			}
		} catch (UnsupportedEncodingException e) {
		}
		return sbFile;
	}
	
	//提取字符串
	public static Set<String> regx(String content, String regx) {
		if (content == null || regx == null) return null;
		Pattern p = Pattern.compile(regx);
		Matcher m = p.matcher(content);
		Set<String> strs = new HashSet<>();
		while (m.find()) {
			int count = m.groupCount();
			for(int i = 0;i < count;i++){
				strs.add( m.group(1));
			}
		}
		return  strs;
	}
	
	public static void printlog(Object msg){
		System.out.println(msg);
	}
	
}
